#include "PinholeCamera.h"
#include <math.h>

PinholeCamera::PinholeCamera(void)
{
}

PinholeCamera::PinholeCamera( Point3* eye, Point3* target, Vector3* up, double fov, int width, int height )
{
	this->imageWidth = width;
	this->imageHeight = height;
	this->eye = new Point3(eye);
	basis = OrthoNormalBasis::makeFromWV(*(Point3::sub(*eye, *target, new Vector3())), *up);
	au = tan((fov * PI / 180.0) / 2.0);
	av = height * au / width;
}
PinholeCamera::~PinholeCamera(void)
{
}

Ray* PinholeCamera::getRay( double x, double y, double lensX, double lensY )
{
	double du = -au + 2.0 * au * x / (getImageWidth() - 1.0);
	double dv = -av + 2.0 * av * y / (getImageHeight() - 1.0);
	return new Ray(eye, basis->transform(Vector3(du, dv, -1), new Vector3()));
}

Point2* PinholeCamera::getPoint( Point3 p )
{
	Vector3* v = new Vector3();
	basis->untransform(*(Point3::sub(p, eye, new Vector3())), v);
	if (v->z > 0.0)
		return 0;
	Point2* sp = new Point2();
	sp->x = (v->x / (-v->z) + au) * (getImageWidth() - 1.0) / (2.0 * au);
	sp->y = (v->y / (-v->z) + av) * (getImageHeight() - 1.0) / (2.0 * av);
	return sp;
}